#!/usr/bin/env python
import time
import random
from visual import *
def RR(val):
	return (val*random.uniform(0,1),val*random.uniform(0,1),val*random.uniform(0,1))

class Scrollgraph:
    def __init__(self,xlabel='x',ylabel='y'):
        self.scene = display(title="Test",background=color.white,x=600,width=600,height=400,autoscale=False)
        #create the curves
	self.xlabel=xlabel
	self.ylabel=ylabel
        self.gridon = True
        self.cartesio = frame() #frame degli assi
        self.tacchette=frame()#frame delle tacchette degli assi
	self.cur=frame()#frame delle curve
	self.docartesio()
	self.curarra=[]
	self.intervalx(0,24,2,4)
	self.intervaly(-12,12,3,1)
        
    def ncen(self,ncenter):
	self.scene.center=ncenter
	#il mistero dei misteri: la finbestra va da -12 a +12 sia su x sia su y 0_0
        self.cartesio.pos=self.scene.center-(12,0)
	self.tacchette.pos= self.cartesio.pos
	#self.scene.range=ncenter[0]
        
    def plot(self,index,point):
	self.scene.select()
	self.curarra[index].append(point)
	#print scene.mouse.pos
	
    def docartesio(self):
	 arrow(frame=self.cartesio,pos=(0,0,-1),length=24,axis=(1,0,0),fixedwidth = 1, shaftwidth=0.1, color=color.red)
	 arrow(frame=self.cartesio,pos=(0,0,-1),length=7.5, axis=(0,1,0),fixedwidth = 1, shaftwidth=0.1, color=color.blue)
	 box(  frame=self.cartesio,pos=(0,-3.75,-1),axis=(0,1,0), length=7.5, height=0.1, width=0.1, color=color.blue) #il 'continuo' dell'asse y
	 label(frame=self.cartesio,pos=(23,-1,-1),text=self.xlabel,color=color.orange,box=0, line=0)
	 label(frame=self.cartesio,pos=(1,7,-1),text=self.ylabel,color=color.orange,box=0, line=0)


    def addcurva(self):
	self.scene.select()
	tmp=curve(frame=self.cur,pos=(0,0),color=RR(0.8))
	self.curarra.append(tmp)
	return self.curarra.index(tmp)
	
    def intervalx(self,minx,maxx,sep,lab=0):
		self.scene.select()
		for i in range(minx, maxx):
			if not(i%sep):
				box(frame=self.tacchette,pos=(i,0,-1),axis=(1,0,0), length=0.1, height=0.5, width=0.1, color=color.red)
		 	if lab!=0 and not(i%lab):
				 label(frame=self.tacchette,pos=(i,-0.6,-1),text=str(i), height=8,opacity=0,color=color.red,box=0, line=0)

    def intervaly(self,miny,maxy,sep,lab=0):
		self.scene.select()
		for i in range(miny, maxy):
			if not(i%sep):
				box(frame=self.tacchette,pos=(0,i,-1),axis=(0,1,0), length=0.1, height=0.5, width=0.1, color=color.blue)
		 	if lab!=0 and not(i%lab):
				 label(frame=self.tacchette,pos=(0.6,i,-1),text=str(i), height=8,opacity=0,color=color.blue,box=0, line=0)

